package edu.must.uh.util;

import org.apache.log4j.Logger;

public class ToolsOfString {   
    
    private static Logger logger = Logger.getLogger(ToolsOfString.class); 
    
    public static String NulltoEmpty(Object str) {
        if (str == null) {
            str = "";
        }  
        return str.toString().trim();
    }
    
    
    public static String conveString(Object strTemp) {
        
        if(strTemp == null)
        {
           return "";
        }
        if(strTemp.toString().trim().toUpperCase().equals("undefined".toUpperCase()))
        {
            return "";
        }  
        if(strTemp.toString().trim().equals("null"))
        {
            return "";
        }
        if(sqlValidate(strTemp.toString().trim()))
        {
            return "";
        }
        return strTemp.toString().trim().replace("&nbsp;", "");     
    }
    
    //效验
    protected static boolean sqlValidate(String str) {
        str = str.toLowerCase();//统一转为小写       
        String badStr = "exec|execute|insert|select|delete|update|count|drop|chr|master|truncate|" +
                "char|declare|sitename|net user|xp_cmdshell|like|exec|execute|insert|create|drop|" +
                "table|grant|group_concat|column_name|" +
                "information_schema.columns|table_schema|union|where|" +
                "chr|master|truncate|declare|";
        String[] badStrs = badStr.split("\\|");
        for (int i = 0; i < badStrs.length; i++) {
            if (str.indexOf(badStrs[i]) >= 0) {
                logger.info("sqlValidate 失败:" + str);
                return true;
            }
        }
        return false;
    }

}
